package com.muqi.app.qlearn.mine;

import java.util.HashMap;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import u.aly.bt;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyAmendPwdActivity extends BaseFragmentActivity implements
		OnClickListener {
	ImageView jinwai_fanhui_jinwai;
	EditText old_pwd, update_pwd, re_update_pwd;
	Button que_update_pwd;
	CheckBox mCheckBox;
	int uiType;
	private String oldPws, newPws, reNewPws;
	private TextView tv_title;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_amend_pwd);

	}

	public void AmendPwdHttp() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());

		sendMap.put("oldpassword", oldPws);
		sendMap.put("newpassword", newPws);
		String requestUrl = ParamsUtils.buildParams(NetWorkApi.MODIFY_PASS,
				sendMap);

		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				Log.e("===", result);
				que_update_pwd.setEnabled(true);
				if (resuleCode == RESULTCODE.SUCCESS) {
					showToast("修改成功");
					finish();
				} else {
					try {
						JSONObject resultAllata = null;
						resultAllata = new JSONObject(result);
						showToast(resultAllata.getString("data"));

					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

			}
		});

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.que_update_pwd:
			if (checkInput()) {
				que_update_pwd.setEnabled(false);
				AmendPwdHttp();
			}

			break;

		default:
			break;
		}
	}

	private boolean checkInput() {
		oldPws = old_pwd.getText().toString();
		newPws = update_pwd.getText().toString();
		reNewPws = re_update_pwd.getText().toString();
		if (TextUtils.isEmpty(oldPws)) {
			old_pwd.setError("请输入旧密码");
			return false;
		} else if (TextUtils.isEmpty(newPws)) {
			update_pwd.setError("请输入新密码");
			return false;
		} else if (TextUtils.isEmpty(reNewPws)) {
			re_update_pwd.setError("请输入确认密码");
			return false;
		} else if (!newPws.equals(reNewPws)) {
			re_update_pwd.setError("两次密码不一致");
			return false;
		} else

			return true;
	}

	public void onBack(View v) {
		finish();
	}

	@Override
	protected void onInit() {
		old_pwd = (EditText) findViewById(R.id.old_pwd);
		update_pwd = (EditText) findViewById(R.id.update_pwd);
		re_update_pwd = (EditText) findViewById(R.id.re_update_pwd);
		que_update_pwd = (Button) findViewById(R.id.que_update_pwd);
		tv_title = (TextView) findViewById(R.id.tv_title);
		que_update_pwd.setOnClickListener(this);

	}

	@Override
	protected void onData() {
		uiType = getIntent().getIntExtra("uiType", 1);
		if (uiType == 1) {
			tv_title.setText("修改登录密码");
		} else {
			tv_title.setText("修改支付密码");
			old_pwd.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
			old_pwd.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
					6) });
			update_pwd.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
			update_pwd
					.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
							6) });
			re_update_pwd.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
			re_update_pwd
					.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
							6) });
		}

	}
}
